package com.tenement.action;

import com.tenement.domain.TenementClient;
import com.tenement.domain.TenementUser;
import com.tenement.service.ClientService;
import com.tenement.utils.FileTools;
import com.tenement.utils.HoseOrClientVo;
import com.tenement.utils.Message;
import com.tenement.utils.Tools;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName : ClientAction  //类名
 * @Description : 客户Controller  //描述
 * @Author : 易嵘  //作者
 * @Date: 2020-06-06 22:48  //时间
 */
@Controller
@RequestMapping("clientAction")
public class ClientAction {

    /**
     * 客户服务接口
     */
    @Resource
    ClientService clientService;

    @Resource
    FileTools fileTools;
    /**
     * 查询所有（分页，条件）
     *
     * @param client
     * @param page
     * @param limit
     * @return
     */
    @ResponseBody
    @RequestMapping("findAll")
    public Map<String, Object> findAll(TenementClient client,
                                       @RequestParam(value = "startDate", required = false) String startDate,
                                       @RequestParam(value = "endDate", required = false) String endDate,
                                       @RequestParam("page") Integer page,
                                       @RequestParam("limit") Integer limit) {

        List<TenementClient> list = clientService.selectAll(client, page, limit, startDate, endDate);

        Long overCount = clientService.overCount(client, startDate, endDate);

        return Tools.maps(overCount, list);
    }

    @ResponseBody
    @RequestMapping("overCount")
    public Long overCount(){
        return clientService.overCount(null,null,null);
    }

    @ResponseBody
    @RequestMapping("findById")
    public TenementClient findById(String id){
        return clientService.findById(id);
    }

    /**
     * 新增Or修改
     *
     * @param client 客户模型
     * @return
     */
    @ResponseBody
    @RequestMapping("addOrUpdate")
    public Message addOrUpdate(TenementClient client, HttpSession session, HttpServletRequest request) {
        int row;
        String ip = request.getRemoteAddr(); //取登陆人的ip
        TenementUser user = (TenementUser) session.getAttribute("user");
        if (StringUtils.isEmpty(client.getClientId())) {
            row = clientService.add(client, user,ip);
        } else {
            row = clientService.update(client, user,ip);
        }
        return Tools.rows(row);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteById")
    public Message deleteById(String id,HttpServletRequest request) {
        HttpSession session=request.getSession();
        TenementUser user= (TenementUser) session.getAttribute("user");
        String ip = request.getRemoteAddr(); //取登陆人的ip
        int row = clientService.deleteById(id,user,ip);
        return Tools.rows(row);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteAll")
    public Message deleteAll(@RequestParam(value = "ids[]", required = false) String ids[],HttpServletRequest request) {
        String ip = request.getRemoteAddr(); //取登陆人的ip
        HttpSession session=request.getSession();
        TenementUser user= (TenementUser) session.getAttribute("user");
        int row = clientService.deleteAll(ids,user,ip);
        return Tools.rows(row);
    }

    /**
     * @return
     */
    @ResponseBody
    @RequestMapping("selectAll")
    public List<TenementClient> selectAll() {
        return clientService.findAll();
    }

    /**
     * 根据电话查询
     * @param clientPhone
     * @return
     */
    @ResponseBody
    @RequestMapping("findPhone")
    public int findPhone(String clientPhone,String clientId){
        return clientService.findPhone(clientPhone,clientId);
    }

    @ResponseBody
    @RequestMapping("/uploadNew")
    public Map<String,Object> uploadNew(@RequestParam(value = "file") MultipartFile imgFile, HttpServletRequest request) throws IOException {
        Map<String,Object> map=new HashMap<>();
        Map<String,Object> fileMap=new HashMap<>();
        fileMap.put("src",fileTools.saveFile("client",imgFile));
        map.put("code", 0);
        map.put("msg", "");
        map.put("data",fileMap);
        return map;
    }



    /**
     * 日期格式化
     *
     * @param binder
     */
    /*@InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); // true:允许输入空值，false:不能为空值

    }*/
}
